package com.example.springboot.service.impl;

import com.example.springboot.entity.Book;
import com.example.springboot.entity.BookReservation;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.BookMapper;
import com.example.springboot.mapper.BookReservationMapper;
import com.example.springboot.service.IBookReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author 30473
* @description
* @createDate 2025-10-24 20:31:41
*/
@Service
public class BookReservationService implements IBookReservationService {

    @Autowired
    private BookReservationMapper bookReservationMapper;
    @Autowired
    private BookMapper bookMapper;

    /**
     * 修改预约信息
     * @param reservation
     * @return
     */
    @Override
    public int updateReservation(BookReservation reservation) {
        return bookReservationMapper.updateReservation(reservation);
    }


    /**
     * 新增预约信息
     * @param bookReservation
     */
    @Transactional
    public void save(BookReservation bookReservation) {
        // 1. 校验图书是否存在
        Book book = bookMapper.getById(bookReservation.getBookId());
        if (Objects.isNull(book)) {
            throw new ServiceException("图书不存在");
        }
        // 2. 校验可预约图书数量
        if (book.getNums()< 1) {
            throw new ServiceException("图书库存不足，无法预约");
        }
        // 3. 更新图书的数量
        book.setNums(book.getNums() - 1);
        bookMapper.updateById(book);

        //4. 设置默认状态
        bookReservation.setStatus("pending");//待处理

        // 5. 设置预约时间
        bookReservation.setReservationDate(new Date());
        // 6. 设置取书截止时间（默认3天后）
        LocalDateTime deadline = LocalDateTime.now().plusDays(3);
        Date pickupDeadline = Date.from(deadline.atZone(ZoneId.systemDefault()).toInstant());
        bookReservation.setPickupDeadline(pickupDeadline);

        // 7. 新增预约记录
        bookReservationMapper.save(bookReservation);
    }

    /**
     * 根据用户ID查询预约记录
     * @param userId
     * @return
     */
    @Override
    public List<BookReservation> getReservationsByUserId(Long userId) {
        return bookReservationMapper.findByUserId(userId);
    }
/**
     * 根据图书ID查询预约记录
     * @param bookId
     * @return
     */
    @Override
    public List<BookReservation> getReservationsByBookId(Long bookId) {
        return bookReservationMapper.findByBookId(bookId);
    }
/**
     * 根据用户ID和状态查询预约记录
     * @param userId
     * @param status
     * @return
     */
    @Override
    public List<BookReservation> getReservationsByUserIdAndStatus(Long userId, String status) {
        return bookReservationMapper.getReservationsByUserIdAndStatus(userId, status);
    }

    /**
     * 根据状态查询预约记录
     * @param status
     * @return
     */
    @Override
    public List<BookReservation> getReservationsByStatus(String status) {
        return bookReservationMapper.getReservationsByStatus(status);
    }


}




